package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.ShoppingOrderMapper;
import com.jiyun.pojo.AddManage;
import com.jiyun.pojo.ShoppingOrder;
import com.jiyun.pojo.Shoppingcart;
import com.jiyun.service.ShoppingOrderService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ShoppingOrderServiceImpl extends ServiceImpl<ShoppingOrderMapper, ShoppingOrder> implements ShoppingOrderService {

    @Autowired
    ShoppingOrderMapper shoppingOrderMapper;

    @Override
    public R findShoppingOrder(ShoppingOrder shoppingOrder) {
        List<ShoppingOrder> list = shoppingOrderMapper.findShoppingOrders(shoppingOrder);
        return R.ok().data("list", list);
    }

    @Override
    public R cancelorder(String id) {
//        ShoppingOrder shoppingOrder = shoppingOrderMapper.cancelorder(id);
        shoppingOrderMapper.updatecancelorder(id);
        return R.ok();
    }

    @Override
    public R deleteorder(String id) {
        shoppingOrderMapper.deleteorder(id);
        return R.ok();
    }


    @Override
    public R add(ShoppingOrder shoppingOrder, String s, Integer quantity, Integer totalAmount, Integer shippingFee, Integer deliveryMethod, String orderNote, String name, String tel, String address,Integer aid) {
        shoppingOrder.setId(s);
        shoppingOrder.setNumber(quantity);
        shoppingOrder.setPriceorder(BigDecimal.valueOf(totalAmount));
        shoppingOrder.setFreight(shippingFee);
        shoppingOrder.setCreatedata(new Date());
        shoppingOrder.setShithods(deliveryMethod);
        shoppingOrder.setOrdernotes(orderNote);
        shoppingOrder.setDingname(name);
        shoppingOrder.setPhone(tel);
        shoppingOrder.setAddress(address);
        shoppingOrder.setAid(aid);
        shoppingOrderMapper.insert(shoppingOrder);
        return R.ok();
    }

    @Override
    public R updatestatus(String id) {
        shoppingOrderMapper.updatestatus(id);
        return R.ok();
    }

    @Override
    public R updatebz(String id, String bz) {
        shoppingOrderMapper.updatebz(id, bz);
        return R.ok();
    }

    @Override
    public R findshoppingorderById(String id) {
        ShoppingOrder shoppingOrder = shoppingOrderMapper.findshoppingorderById(id);
        return R.ok().data("shoppingOrder", shoppingOrder);
    }

    @Override
    public R delete(String id) {
        shoppingOrderMapper.deleteShopping(id);
        return R.ok();
    }

    public List<ShoppingOrder> findshoppingbyid(String[] id) {
// 创建一个集合用来存储查询结果
        List<ShoppingOrder> orders = new ArrayList<>();

        // 遍历传入的ID数组，逐个查询
        for (String ids : id) {
            // 假设你有一个查询方法按ID获取订单
            ShoppingOrder order = shoppingOrderMapper.findShoppingById(ids);
            if (order != null) {
                orders.add(order);
            }
        }
        // 返回查询到的订单集合
        return orders;
    }






}
